<template>
  <div>
    <el-row class="top">
      <el-col :span="2"
        >共<span class="num">{{ num }}</span
        >条</el-col
      >
      <el-col :span="3" :offset="19"
        ><div class="btn" @click="release">
          <i class="iconfont icon-bianji"></i>发布经验
        </div></el-col
      >
    </el-row>
    <div class="exp">
      <div v-for="(item, index) in expList" :key="index">
        <div class="box">
          <div class="maincont">
            <el-row>
              <el-col :span="5">
                <router-link to="/main/MyHome/myall">
                  <el-avatar
                    size="medium"
                    :src="item.headImg"
                    class="img"
                  ></el-avatar>
                  <span class="username">{{ item.author }}</span>
                </router-link>
              </el-col>
              <el-col :span="5" :offset="14" class="time">{{
                item.createTime
              }}</el-col>
            </el-row>
            <h3>
              {{ item.title }}
              <span v-for="item2 in item.label" :key="item2">{{ item2 }}</span>
            </h3>
            <div v-if="!item.isZhangkai">
              <div>
                <div class="text" v-html="item.content"></div>
              </div>
              <div class="zhankai" @click="zhankai(item.id)">
                展开<span>(展开可显示图片)</span>
              </div>
            </div>
            <div v-else>
              <div>
                <div class="text2" v-html="item.content"></div>
                <div class="imgBox" v-if="item.imgArr.length > 0">
                  <div v-for="(item, index) in item.imgArr" :key="index">
                    <img :src="item" alt="" />
                  </div>
                </div>
                <div class="zhankai" @click="shouqi(item.id)">
                  收起<span></span>
                </div>
              </div>
            </div>
          </div>

          <div class="botcont">
            <el-row>
              <el-col :span="5" :offset="16" class="time">{{
                item.time
              }}</el-col>
              <el-col :span="3"
                ><el-button type="primary" @click="fabu(item)"
                  >发布</el-button
                ></el-col
              >
            </el-row>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      num: 0,
      expList: [],
      page: null,
    };
  },
  methods: {
    shouqi(id) {
      this.changeItem({ id: id, key: "isZhangkai", value: false });
    },
    zhankai(id) {
      this.changeItem({ id: id, key: "isZhangkai", value: true });
    },
    changeItem(obj) {
      //obj格式{id:1,key:1,value:1}
      console.log(obj);
      this.expList.forEach((item) => {
        if (item.id == obj.id) {
          if (
            obj.key == "isZan" ||
            obj.key == "isShouCang" ||
            obj.key == "isShowCom"
          ) {
            item[obj.key] = !item[obj.key];
          } else {
            item[obj.key] = obj.value;
          }
        }
      });
    },
    release() {
      this.$router.push("/main/releaseexp");
    },
    fabu(obj) {
      //发布经验
      // console.log(this.title,this.imgArr,this.inputBlog.bcontext,this.choseArr.join(','))
      let { nickname, headImg, id } = this.$getStorage("userinfo");
      this.$axios({
        method: "post",
        url: "/experience/upsert",
        data: { ...obj, status: true },
      }).then((res) => {
        console.log(res);
        if (res.code != "200") {
          this.$message.error(res.msg);
        } else {
          this.$message.success("添加成功");
          this.$router.go(-1);
        }
      });
    },
  },
  created() {
    // this.expList = this.$store.state.exp.expList;
    this.$axios({
      url: "/experience/selectByType",
      method: "get",
      params: {
        author: this.$getStorage("userinfo").nickname,
        page: 1,
        size: 6,
        userId: this.$getStorage("userinfo").id,
        status: 0,
      },
    }).then((res) => {
      console.log(res);
      if (res.code == "200") {
        this.page = 1;
        this.expList = res.data.map((item) => ({
          ...item,
          isZhangkai: false,
        }));
        this.num = res.data.length;
      }
    });
  },
};
</script>

<style lang="less" scoped>
@import url("../less/expitem.less");
.top {
  line-height: 40px;
  margin-bottom: 20px;
  .num {
    font-size: 25px;
    color: #312a6d;
  }
  .btn {
    height: 40px;
    background-color: #312a6d;
    border-radius: 5px;
    color: #f2f7fb;
    line-height: 40px;
    font-size: 18px;
    i {
      margin-right: 5px;
    }
  }
}

.moreBtn {
  background-color: #9ba5bb;
  color: #ddd;
  width: 300px !important;
  height: 50px;
  border-radius: 10px;
  line-height: 50px;
  font-size: 20px;
  user-select: none;
  margin: 0 auto;
  &:hover {
    color: white;
  }
}
</style>